<?php

namespace App\Http\Controllers\Admin\Member;

use App\Http\Controllers\Admin\BaseController;
use App\Repositories\MemberRepositoryEloquent;
use Toastr,Config,Validator,Log;
use Illuminate\Http\Request;
use App\Http\Requests\Admin\Member\UpdateRequest;
use App\Models\FinancialDetails;

class MemberController extends BaseController{

    protected $user;

    public function __construct(MemberRepositoryEloquent $user)
    {
        parent::__construct();
        $this->user = $user;


    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {   
        $where = [];
        $whereHas = [];
        if($request->has('mobile') && !empty($request['mobile'])){
            $where[] = ['mobile','like','%'.$request['mobile'].'%'];
        }
        if($request->has('name') && !empty($request['name'])){
            $where[] = ['name','like','%'.$request['name'].'%'];
        }
        if($request->has('roleid') && !empty($request['roleid'])){
            $whereHas[] = ['role_id','=',$request['roleid']];

            $users = $this->user
                ->where($where)
                ->whereHas('roles',function($query) use($whereHas){
                            $query->where($whereHas);
                })->orderBy('balance','desc')->paginate(10);
        }else{
            $users = $this->user
                ->where($where)
                ->orderBy('balance','desc')->paginate(10);
        }
        
        return view('admin.member.index', compact('users'));
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = $this->user->find($id);
        return view('admin.member.edit', compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRequest $request, $id)
    {
        Log::info('更新用信息.');
        $result = $this->user->update($request->all(), $id);
        if(!$result['status']) {
            Toastr::error($result['msg']);
        } else {
            Toastr::success('用户更新成功');
        }
        return redirect(route('admin.member.edit', ['id' => $id]));
    }


    /**
     * [更改用户金额]
     * @return [type] [description]
     */
    public function balance($id){
         $user = $this->user->find($id);
         $financialDetails = $user->financialDetails()->orderBy('id','desc')->paginate(5);
         return view('admin.member.balance',compact('user','financialDetails'));
    }

    public function updateBalance(Request $request,$user_id){
        $validator = Validator::make($request->all(), [
            'money' => 'required|numeric',
            'note' => 'required|max:255',
        ],['money.required'=>'请填写正确的金额','money.numeric'=>'请填写正确的金额','note.required'=>'请填写备注']);

        if ($validator->fails()) {
            Toastr::error($validator->errors()->first());
            return redirect(route('admin.member.balance', ['id' => $user_id]))->withInput();
        }
        $result = $this->user->modifyBalance($user_id,$request->money,6,$request->note);
        if(!$result['status']) {
            Toastr::error($result['msg']);
        } else {
            Toastr::success('金额更新成功');
        }
        return redirect(route('admin.member.balance', ['id' => $user_id]));
    }

    /**
     * [subs 查看下级]
     * @return [type] [description]
     */
    public function subs($id){
        $user = $this->user->find($id);
        //dd($user->ancestors()->get()->all());
        $subs = $user->descendantsAndSelf()->withoutSelf()->get();
        return view('admin.member.subs',compact('user','subs'));
    }

}